package com.jerry.template.module.mianpage.shouye.recommend.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


import com.google.gson.Gson;
import com.jerry.template.R;
import com.jerry.template.base.BaseFragment;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mianpage.shouye.recommend.home.activity.UserDetailActivity;
import com.jerry.template.module.mianpage.shouye.recommend.home.adapter.RecycleViewAdapter;
import com.jerry.template.module.mianpage.shouye.recommend.home.entity.TestDetail;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


/**
 * Created by Administrator on 16-5-9.
 */
public class RecommendFragment extends BaseFragment implements RecycleViewAdapter.OnRecyclerViewListener {

    @BindView(R.id.recommend_recyclerView)
    public RecyclerView recyclerView;

    private RecycleViewAdapter adapter = null;
    private List<TestDetail.DataBean.DataBean1> list;
    private TestDetail testDetail;

    // 务必实现空参数构造方法
    public RecommendFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.recommend_content;
    }

    // 获取参数
    @Override
    protected void getReqParams(Bundle bundle) {
        String tag = bundle.getString("tag");
    }

    @Override
    protected void initViews() {
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        adapter = new RecycleViewAdapter(getContext(), list);
        adapter.setOnRecyclerViewListener(this);
        recyclerView.setAdapter(adapter);

    }

    //加载推荐首页
    @Override
    protected void loadData() {
//        Map<String, Object> param = new HashMap<>();
//        param.put("pageindex", 3);
        OkHttpUtils.postSubmitForm(Urls.MAIN_PAGE_RECOMMEND_URL, null);
    }

    //访问网络后获取的JSON格式的String
    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        super.postEventResult(message);

        if (message != null) {
            if (message.type.equals(Urls.MAIN_PAGE_RECOMMEND_URL)) {
                String result = message.result;
                Gson gson = new Gson();
                testDetail = gson.fromJson(result, TestDetail.class);
                if (testDetail == null || testDetail.getData() == null) {
                    return;
                }
                list.addAll(testDetail.getData().getData());
                adapter.notifyDataSetChanged();
            }
        }
    }

    private int userPositon;
    public int getUserPosition(){
        return userPositon;
    }
    public void setUserPositon(int positon){
        this.userPositon=positon;
    }
    @Override
    public void onItemClick(int position, int viewId) {
        switch (viewId) {
            case R.id.shareNum:
                shareContent(position);
                break;
            case R.id.user_head:
                showUserDetail(position);
                break;
            case R.id.title:
                showUserDetail(position);
                break;
        }


    }

    private void showUserDetail(int position) {


        Intent i = new Intent();
        i.setClass(getContext(), UserDetailActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("user_id",testDetail.getData().getData().get(position).getGroup().getUser().getUser_id()+"");
        startActivity(i);
    }



    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }


    private void shareContent(int position) {
        List<TestDetail.DataBean.DataBean1> dataBean1 = testDetail.getData().getData();
        ShareSDK.initSDK(getContext());
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(dataBean1.get(position).getGroup().getUser().getName());
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(dataBean1.get(position).getGroup().getShare_url());
        // text是分享文本，所有平台都需要这个字段
        oks.setText(dataBean1.get(position).getGroup().getContent());
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片

//        // url仅在微信（包括好友和朋友圈）中使用
//        oks.setUrl("http://sharesdk.cn");

        oks.setImageUrl("http://s0.pstatp.com/resource/neihan_web/static/image/site-v2/joke_jokeessay_new_c0cceed.png");

        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(dataBean1.get(position).getGroup().getShare_url());

        // 启动分享GUI
        oks.show(getContext());
    }


}
